package com.wfk.plat.controller;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.alibaba.fastjson.JSONObject;
import com.image.common.RestUploadFileInfo;
import com.image.common.Service;
import com.wfk.constant.PathConstant;
import com.wfk.plat.mapping.MappingUtils;
import com.wfk.utils.ResponseData;

@Controller
public class FileController {

	@ResponseBody
	@RequestMapping(value=MappingUtils.upload,method= {RequestMethod.POST})
	public ResponseData upload(MultipartFile file) throws IOException {
		if(file == null) {
			return ResponseData.baseError("上传失败") ;
		}
		RestUploadFileInfo upload = com.image.common.Service.uploadImage(file.getOriginalFilename(), file.getInputStream(), PathConstant.bank_image);
		if(upload.isStatus()) {
			return ResponseData.ok().putDataValue("imagePath", upload.getServiceName() + upload.getFilePath() + upload.getFileName()) ;
		}else {
			return ResponseData.baseError("图片上传失败") ;
		}
	}
	
	//ueditr后端配置
		@ResponseBody
		@RequestMapping(value=MappingUtils.ueditor_config,headers = "Accept=application/json")
		public String ueditor(@RequestParam("action") String action, @RequestParam("noCache") String nocache, HttpServletRequest request, HttpServletResponse response){
			try {
				response.setContentType("application/json;charset=utf-8");
				Resource resource = new ClassPathResource("config.json");
				File file = resource.getFile();
				BufferedReader br = new BufferedReader(new FileReader(file));
				StringBuilder stringBuilder = new StringBuilder();
				String line;
				while ((line = br.readLine()) != null){
					stringBuilder.append(line);
				}
				return stringBuilder.toString();
			}catch (Exception e){
				e.printStackTrace();
				return "error";
			}
		}
	
	@RequestMapping(value = MappingUtils.neditorImage)
	@ResponseBody
	public Object uploadimage(MultipartFile file, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		if (file != null) {
			try {
				RestUploadFileInfo r = Service.upload(file.getOriginalFilename(), file.getInputStream(), PathConstant.sysnews);
				if (!r.isStatus()) {
					json.put("code", 500);
					json.put("msg", "上传失败");
				} else {
					json.put("msg", "上传成功");
					json.put("code", 200);
					json.put("url", r.getServiceName() + r.getFilePath() + r.getFileName());
					json.put("size", file.getSize());
				}
			} catch (Exception e) {
				json.put("code", 500);
				json.put("msg", "上传失败");
			}
		}
		return json;
	}
}
